import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class JisuanqiFrame extends JFrame {


    public JisuanqiFrame() {
        JTextField textfield;
        textfield=new JTextField(20);
        Container c=this.getContentPane();
        c.add(textfield);
        c.setLayout(new FlowLayout(1, 10, 10));
        Jisuanqi mytext=new Jisuanqi();


        Dimension preferredSize=new Dimension(50, 50);
        String buttontext[]=new String[]{"<*", "CE", "%", "+", "1", "2", "3", "-", "4", "5", "6", "*", "7", "8", "9", "/", "", "0", ".", "=",};
        for (int i=0; i < buttontext.length; i++) {
            JButton button=new JButton(buttontext[i]);
            int finalI=i;
            if (buttontext[i].equals("<*")) {
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String TEXT=textfield.getText();
                        if (TEXT.length() - 1 >= 0) {
                            TEXT=TEXT.substring(0, TEXT.length() - 1);
                        }
                        textfield.setText(TEXT);
                    }
                });
            } else if (buttontext[i].equals("CE")) {
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        textfield.setText("");
                    }
                });
            } else if (buttontext[i].equals("=")) {
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Double results=mytext.getResults(textfield.getText());
                        String result=String.valueOf(results);
                        textfield.setText(textfield.getText()+"="+result);

                    }
                });
            } else {
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String text=textfield.getText();
                        String texts=text.concat(buttontext[finalI]);
                        textfield.setText(texts);

                    }
                });
            }
            button.setPreferredSize(preferredSize);
            c.add(button);
        }
        setSize(260, 400);
        setVisible(true);
    }


    public static void main(String[] args) {
        JisuanqiFrame mytest=new JisuanqiFrame();
        JButton jb=new JButton();
    }

}


